package com.qj.daibei.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * author wml
 * Created on 2021/10/7 23:00
 */
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String uri = request.getRequestURI();
        System.out.println(uri);
        if ("/".equals(uri) || uri.indexOf("login") != -1 || uri.indexOf("zhuce") != -1 || uri.indexOf("index") != -1) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        HttpSession session = request.getSession(false);
        if (session != null) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        request.getRequestDispatcher("/jump?url=login").forward(servletRequest, servletResponse);
    }
}
